﻿/* ****************************************************************************
* Copyright (c) TCC - 2009 IME - USP. 
*
* This source code is subject to terms and conditions of the Microsoft Public License. 
* A copy of the license can be found in http://msdn.microsoft.com/en-us/library/dd228915.aspx. 
* By using this source code in any fashion, you are agreeing to be bound by the terms of the 
* Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
* ***************************************************************************/

using System.IO;
using System;

namespace SoundLibrary.Converter
{
    public class Mp3Stream
    {
        private Stream sourceStream;
        private byte[] temp = new byte[1000];
        
        public Mp3Stream(string fileName)
        {
            this.sourceStream = new FileStream(fileName, FileMode.Open);
        }

        public Stream SourceStream
        {
            get { return sourceStream; }
        }

        public int Read(sbyte[] syncbuffer, int length)
        {
            return this.Read(syncbuffer, 0, length);
        }

        public int Read(sbyte[] syncbuffer, int writeOffset, int length)
        {
            int bytesRead;
            bytesRead = this.sourceStream.Read(this.temp, writeOffset, length);
            for (int i = writeOffset; i < writeOffset + length; i++)
            {
                syncbuffer[i] = (sbyte)temp[i];
            }
            
            return bytesRead;
        }

        internal void UnRead(int read)
        {
            sourceStream.Position -= read;
        }

        internal void Forward(int framesize)
        {
            this.sourceStream.Position += framesize;
        }

        internal void Rewind(int framesize)
        {
            this.sourceStream.Position -= framesize;
        }
    }
}
